import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class BottomSheetWidget extends StatefulWidget {
  final double? height;
  final Widget? customChild;
  final String? secondPartText;
  final bool isSecondPartHidden;
  final TextAlign firstPartTextAlign;

  final String? firstPartText;

  const BottomSheetWidget({
    super.key,
    this.height,
    this.customChild,
    this.secondPartText,
    this.isSecondPartHidden = false,
    required this.firstPartText,
    this.firstPartTextAlign = TextAlign.center,
  });

  @override
  State<BottomSheetWidget> createState() => _BottomSheetWidgetState();
}

class _BottomSheetWidgetState extends State<BottomSheetWidget> {
  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: const Duration(milliseconds: 300),
      height: widget.height ?? MediaQuery.of(context).size.height * 1,
      decoration: const BoxDecoration(
        color: Color(0xFF16151A),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(16),
          topRight: Radius.circular(16),
        ),
      ),
      child: Column(
        children: [
          // 第一部分
          Container(
            padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8),
            child: Column(
              children: [
                Container(
                  width: 32.w,
                  height: 4.h,
                  decoration: BoxDecoration(
                    color: const Color(0xFF16151A),
                    borderRadius: BorderRadius.circular(2),
                  ),
                ),
                const SizedBox(height: 8),
                Row(
                  mainAxisAlignment:
                      widget.firstPartTextAlign == TextAlign.center
                      ? MainAxisAlignment.center
                      : MainAxisAlignment.spaceBetween,
                  children: [
                    // 用Expanded包裹文本，使其获得可用空间约束
                    if (widget.firstPartTextAlign != TextAlign.center)
                      Expanded(
                        child: Text(
                          widget.firstPartText!,
                          textAlign: widget.firstPartTextAlign,
                          style: TextStyle(color: Colors.white),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),

                    if (widget.firstPartTextAlign == TextAlign.center)
                      Expanded(
                        child: Text(
                          widget.firstPartText!,
                          textAlign: widget.firstPartTextAlign,
                          style: TextStyle(color: Colors.white),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),

                    // 确保关闭按钮不会被挤压
                    IconButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      icon: const Icon(Icons.close, color: Colors.white),
                    ),
                  ],
                ),
              ],
            ),
          ),
          // 第二部分
          if (!widget.isSecondPartHidden && widget.secondPartText != null)
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 0),
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  widget.secondPartText!,
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),

          // 第三部分
          Expanded(child: widget.customChild ?? const SizedBox()),
        ],
      ),
    );
  }
}
